package net.oschina.jhssm.common.entity;

import java.util.HashMap;

/**
 * Created by zhaojb on 2017/8/20.
 */
public class R extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	private R() {

    }

    private static R handleR(R r, Object... data) {
        if (data.length % 2 != 0) {
            throw new IllegalArgumentException("传入的参数必须是偶数！");
        }
        for (int i=0; i<data.length/2 + 1; i=i+2) {
            if (data[i] instanceof String) {
                r.put(String.valueOf(data[i]), data[i+1]);
            }
        }
        return r;
    }

    public static R ok() {
        R r = new R();
        r.put("code", 0);
        r.put("success", true);
        return r;
    }

    public static R ok(Object... data) {
        R r = R.ok();
        return handleR(r, data);
    }

    public static R error() {
        R r = new R();
        r.put("success", false);
        return r;
    }

    public static R error(String message) {
        R r = R.error();
        r.put("message", message);
        return r;
    }

    public static R error(String message, Object... data) {
        R r = R.error(message);
        return handleR(r, data);
    }

}
